package com.lingdong.onlinejudge.controller;

import com.lingdong.onlinejudge.common.BaseResponse;
import com.lingdong.onlinejudge.common.ErrorCode;
import com.lingdong.onlinejudge.common.ResultUtils;
import com.lingdong.onlinejudge.exception.BusinessException;
import com.lingdong.onlinejudge.model.dto.user.UserLoginByEmailDto;
import com.lingdong.onlinejudge.model.dto.user.UserLoginDto;
import com.lingdong.onlinejudge.model.dto.user.UserRegisterDto;
import com.lingdong.onlinejudge.model.vo.base.CaptchaVo;
import com.lingdong.onlinejudge.model.vo.base.EmailVo;
import com.lingdong.onlinejudge.model.vo.user.UserLoginVo;
import com.lingdong.onlinejudge.model.vo.user.UserVo;
import com.lingdong.onlinejudge.service.AuthService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


/**
 * 认证相关接口
 * @author lzw
 */

@RestController
@RequestMapping("/auth")
public class AuthController {

    @Resource
    private AuthService authService;

    /**
     * 用户登录 (账号密码)
     * @param userLoginDto 用户登录信息
     * @return 结果
     */
    @PostMapping("/login")
    public BaseResponse<UserLoginVo> userLogin(@RequestBody UserLoginDto userLoginDto){
        if (userLoginDto == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        UserLoginVo userLoginVo = authService.userLogin(userLoginDto);
        return ResultUtils.success(userLoginVo);
    }

    /**
     * 用户登录 (邮箱)
     * @param userLoginByEmailDto 用户登录信息
     * @return token
     */
    @PostMapping("/login/email")
    public BaseResponse<UserLoginVo> userLoginByEmail(@RequestBody UserLoginByEmailDto userLoginByEmailDto){
        if (userLoginByEmailDto == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        UserLoginVo loginByEmail = authService.userLoginByEmail(userLoginByEmailDto);
        return ResultUtils.success(loginByEmail);
    }

    /**
     * 获取验证码
     * @return 结果
     */
    @GetMapping("/captcha")
    public BaseResponse<CaptchaVo> captcha(){
        CaptchaVo captcha = authService.captcha();
        return ResultUtils.success(captcha);
    }

    /**
     * 发送邮箱验证码
     */
    @GetMapping("/send/email")
    public BaseResponse<EmailVo> sendEmail(@RequestParam String email){
        if (StringUtils.isAnyBlank(email)){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        EmailVo result = authService.sendEmail(email);
        return ResultUtils.success(result);
    }

    /**
     * 用户注册
     * @param userRegisterDto 用户注册信息
     * @return 结果
     */
    @PostMapping("/register")
    public BaseResponse<Boolean> register(@RequestBody UserRegisterDto userRegisterDto){
        if (userRegisterDto == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Boolean result = authService.register(userRegisterDto);
        return ResultUtils.success(result);
    }

    /**
     * 获取登录用户信息
     * @param token token
     * @return 结果
     */
    @GetMapping("/info")
    public BaseResponse<UserVo> info(@RequestHeader("token") String token){
        if (token == null){
            throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR);
        }
        UserVo userInfo = authService.getUserInfo(token);
        return ResultUtils.success(userInfo);
    }

    /**
     * 退出登录
     * @param token token
     * @return 结果
     */
    @GetMapping("/logout")
    public BaseResponse<Boolean> logout(@RequestHeader("token") String token){
        Boolean result = authService.logout(token);
        return ResultUtils.success(result);
    }
}
